package com.zhaokuner.customer.ctrl;

import com.alibaba.fastjson.JSON;
import com.zhaokuner.parentstarter.ctrladaptor.annotation.CtrlAdaptorAutowired;
import com.zhaokuner.parentstarter.handler.HttpReqInfoUtil;
import com.zhaokuner.parentstarter.utils.MyAppContextUtil;
import com.zhaokuner.provider.service.backup.controller.BackupTipConfirmControllerAdaptor;
import com.zhaokuner.provider.service.backup.model.ao.UserAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Locale;

/**
 * <p>
 * Description:测速
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author 赵希坤 zhaoxikun
 * @date 2021/2/16 23:40
 */
@RestController
@RequestMapping("test")
public class TestCtrl {

    @CtrlAdaptorAutowired
    BackupTipConfirmControllerAdaptor backupTipConfirmControllerAdaptor;


    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping
    public Object test(HttpServletRequest request) throws IOException {

        //System.out.println("customer welcome:" + MyAppContextUtil.getMessage("welcome", null, Locale.US));
        System.out.println("customer seeyou:" + MyAppContextUtil.getMessage("seeyou", null, Locale.CHINA));
        // return backupTipConfirmControllerAdaptor.showTip("http访问", request);
        return null;
    }

    @RequestMapping("update")
    public Object update() {
        Object obj = null;
        Long len = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            UserAO userAO = new UserAO();
            userAO.setName("update test " + System.currentTimeMillis());
            userAO.setAge(11);
            obj = backupTipConfirmControllerAdaptor.update(userAO);
        }
        len = System.currentTimeMillis() - len;
        System.out.println(len);
        return obj;
    }

    @RequestMapping("update2")
    public Object update2() throws NoSuchMethodException {
        Object obj = null;
        Long len = System.currentTimeMillis();
        Type type = backupTipConfirmControllerAdaptor.getClass().getMethod("update", UserAO.class).getGenericReturnType();
        for (int i = 0; i < 1; i++) {
            UserAO userAO = new UserAO();
            userAO.setName("update test " + System.currentTimeMillis());
            userAO.setAge(11);
            HttpHeaders headers = HttpReqInfoUtil.get().getHeaders();
            HttpEntity<Object> requestEntity = new HttpEntity<>(userAO, headers);
            ResponseEntity<String> entity = restTemplate.exchange("http://localhost:8081/provider/backuppolicy/update", HttpMethod.POST, requestEntity, String.class);
            if (HttpStatus.OK.equals(entity.getStatusCode())) {
                obj = JSON.parseObject(entity.getBody(), type);
            }
        }
        len = System.currentTimeMillis() - len;
        System.out.println(len);
        return obj;
    }

}
